<?php

/**
 * This is the model class for table "sys_member".
 *
 * The followings are the available columns in table 'sys_member':
 * @property string $member_network_id
 * @property string $member_name
 * @property string $member_nickname
 * @property string $member_email
 * @property string $member_sex
 * @property string $member_address
 * @property string $member_city_id
 * @property string $member_state_id
 * @property string $member_country_id
 * @property string $member_zipcode
 * @property string $member_birth_place
 * @property string $member_birth_date
 * @property string $member_identity_type
 * @property string $member_identity_no
 * @property string $member_phone
 * @property string $member_fax
 * @property string $member_mobilephone
 * @property string $member_image
 * @property string $member_join_datetime
 * @property string $member_devisor_name
 * @property string $member_devisor_relationship
 * @property string $member_devisor_phone
 * @property string $member_bank_id
 * @property string $member_bank_city
 * @property string $member_bank_branch
 * @property string $member_bank_account_name
 * @property string $member_bank_account_no
 * @property string $member_tax_account_no
 * @property string $member_serial_id
 * @property string $member_register_id
 * @property string $member_last_update
 * @property string $member_last_login
 * @property string $member_is_updated_by_member
 * @property string $member_is_updated_by_admin
 * @property string $member_is_suspended
 *
 * The followings are the available model relations:
 * @property SysSerial $memberSerial
 * @property SysNetwork $memberNetwork
 */
class sys_member extends CActiveRecord {


    public $network_mid;
    public $sponsor_network_id;
    /**
     * Returns the static model of the specified AR class.
     * @return sys_member the static model class
     */

    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'sys_member';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('member_register_id', 'required', 'on'=>'insert'),
                array('member_network_id, member_city_id, member_state_id, member_country_id, member_bank_id', 'length', 'max' => 10),
                array('member_name, member_nickname, member_email, member_zipcode, member_birth_place, member_identity_no, member_phone, member_fax, member_mobilephone, member_image, member_devisor_name, member_devisor_relationship, member_devisor_phone, member_bank_city, member_bank_branch, member_bank_account_name, member_bank_account_no, member_tax_account_no, member_register_id', 'length', 'max' => 255),
                array('member_sex', 'length', 'max' => 6),
                array('member_is_auto_withdrawal', 'length', 'max' => 1),
                array('member_identity_type', 'length', 'max' => 8),
                array('network_mid, sponsor_network_id,member_serial_id', 'length', 'max' => 20),
                array('member_is_updated_by_member, member_is_updated_by_admin, member_is_suspended', 'length', 'max' => 1),
                array('member_address, member_birth_date, member_join_datetime, member_last_update, member_last_login', 'safe'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('network_mid, sponsor_network_id,member_network_id, member_name, member_nickname, member_email, member_sex, member_address, member_city_id, member_state_id, member_country_id, member_zipcode, member_birth_place, member_birth_date, member_identity_type, member_identity_no, member_phone, member_fax, member_mobilephone, member_image, member_join_datetime, member_devisor_name, member_devisor_relationship, member_devisor_phone, member_bank_id, member_bank_city, member_bank_branch, member_bank_account_name, member_bank_account_no, member_tax_account_no, member_serial_id, member_register_id, member_last_update, member_last_login, member_is_updated_by_member, member_is_updated_by_admin, member_is_suspended', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
                'rel_member_to_serial' => array(self::BELONGS_TO, 'sys_serial', 'member_serial_id'),
                'rel_member_to_network' => array(self::BELONGS_TO, 'sys_network', 'member_network_id'),
		'rel_member_to_ref_bank'=>array(self::BELONGS_TO, 'ref_bank', 'member_bank_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'member_network_id' => 'Member Network',
                'member_name' => 'Nama',
                'member_nickname' => 'Nickname',
                'member_email' => 'Email',
                'member_sex' => 'Jenis Kelamin',
                'member_address' => 'Alamat',
                'member_city_id' => 'Kota',
                'member_state_id' => 'Provinsi',
                'member_country_id' => 'Negara',
                'member_zipcode' => 'Kodepos',
                'member_birth_place' => 'Tempat Lahir',
                'member_birth_date' => 'Tanggal Lahir',
                'member_identity_type' => 'Jenis Identitas',
                'member_identity_no' => 'Nomer Identitas',
                'member_phone' => 'No Telepon',
                'member_fax' => 'Fax',
                'member_mobilephone' => 'Nomer HP',
                'member_image' => 'Image',
                'member_join_datetime' => 'Join Datetime',
                'member_devisor_name' => 'Nama Ahli Waris',
                'member_devisor_relationship' => 'Hubungan Waris',
                'member_devisor_phone' => 'No Telepon',
                'member_bank_id' => 'Bank',
                'member_bank_city' => 'Kota Bank',
                'member_bank_branch' => 'Bank Cabang',
                'member_bank_account_name' => 'Nama Rekening',
                'member_bank_account_no' => 'No Rekening',
                'member_tax_account_no' => 'No NPWP',
                'member_serial_id' => 'Serial ID',
                'member_register_id' => 'Register ID',
                'member_last_update' => 'Last Update',
                'member_last_login' => 'Last Login',
                'member_is_updated_by_member' => 'Is Updated By Member',
                'member_is_updated_by_admin' => 'Is Updated By Admin',
                'member_is_suspended' => 'Status',
                'member_is_auto_withdrawal' => 'Auto Withdrawal',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        if(isset($_GET['sys_member'])){

            $get_sys_member=$_GET['sys_member'];
            if(trim($get_sys_member['sponsor_network_id'])!='')
                {
                    $criteria->addCondition('network_sponsor_network_id=(SELECT network_id FROM sys_network WHERE network_mid="'.$get_sys_member['sponsor_network_id'].'")');
                }
        }

        $criteria->with=array('rel_member_to_network');
        $criteria->together=true;
        $criteria->compare('rel_member_to_network.network_mid',$this->network_mid,true);
        $criteria->compare('member_network_id', $this->member_network_id, true);
        $criteria->compare('member_name', $this->member_name, true);
        $criteria->compare('member_nickname', $this->member_nickname, true);
        $criteria->compare('member_email', $this->member_email, true);
        $criteria->compare('member_sex', $this->member_sex, true);
        $criteria->compare('member_address', $this->member_address, true);
        $criteria->compare('member_city_id', $this->member_city_id, true);
        $criteria->compare('member_state_id', $this->member_state_id, true);
        $criteria->compare('member_country_id', $this->member_country_id, true);
        $criteria->compare('member_zipcode', $this->member_zipcode, true);
        $criteria->compare('member_birth_place', $this->member_birth_place, true);
        $criteria->compare('member_birth_date', $this->member_birth_date, true);
        $criteria->compare('member_identity_type', $this->member_identity_type, true);
        $criteria->compare('member_identity_no', $this->member_identity_no, true);
        $criteria->compare('member_phone', $this->member_phone, true);
        $criteria->compare('member_fax', $this->member_fax, true);
        $criteria->compare('member_mobilephone', $this->member_mobilephone, true);
        $criteria->compare('member_image', $this->member_image, true);
        $criteria->compare('member_join_datetime', $this->member_join_datetime, true);
        $criteria->compare('member_devisor_name', $this->member_devisor_name, true);
        $criteria->compare('member_devisor_relationship', $this->member_devisor_relationship, true);
        $criteria->compare('member_devisor_phone', $this->member_devisor_phone, true);
        $criteria->compare('member_bank_id', $this->member_bank_id, true);
        $criteria->compare('member_bank_city', $this->member_bank_city, true);
        $criteria->compare('member_bank_branch', $this->member_bank_branch, true);
        $criteria->compare('member_bank_account_name', $this->member_bank_account_name, true);
        $criteria->compare('member_bank_account_no', $this->member_bank_account_no, true);
        $criteria->compare('member_tax_account_no', $this->member_tax_account_no, true);
        $criteria->compare('member_serial_id', $this->member_serial_id, true);
        $criteria->compare('member_register_id', $this->member_register_id, true);
        $criteria->compare('member_last_update', $this->member_last_update, true);
        $criteria->compare('member_last_login', $this->member_last_login, true);
        $criteria->compare('member_is_updated_by_member', $this->member_is_updated_by_member, true);
        $criteria->compare('member_is_updated_by_admin', $this->member_is_updated_by_admin, true);
        $criteria->compare('member_is_suspended', $this->member_is_suspended, true);

        $sort = new CSort();
        $sort->attributes = array(
                'network_mid'=>array(
                        'asc'=>'(
                SELECT network_mid
                FROM sys_network where network_id = member_network_id
                ) ASC',
                        'desc'=>'(
                SELECT network_mid
                FROM sys_network where network_id = member_network_id
                ) DESC',
                ),
                '*', // add all of the other columns as sortable
        );


        return new CActiveDataProvider($this, array(
                        'criteria' => $criteria,
                        'sort'=>$sort,
        ));
    }

}
